package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.DealerDao;
import com.bed.lovewithpatientbed.dao.WorkerMessageDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.DealerService;
import com.bed.lovewithpatientbed.service.HospitalManagerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author myp
 * @version 1.0.0
 * @ClassName DealerServiceImpl.java
 * @Description 经销商service接口的实现类
 */
@Service
@Transactional
public class DealerServiceImpl implements DealerService {
    @Resource
    private DealerDao dealerDao;
    @Resource
    private WorkerMessageDao workerMessageDao;

    /**
     * @title 新增经销商
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer saveDealer(Dealer dealer) {
        WorkerMessage workerMessage=new WorkerMessage();
        workerMessage.setWorkMessagePhone(dealer.getDealerPhone());
        workerMessage.setWorkMessagePassword("123456");
        workerMessage.setRoleId(dealer.getRole().getRoleId());
        workerMessage.setSoftdel(1);
        workerMessageDao.insertSelective(workerMessage);
        WorkerMessageExample example=new WorkerMessageExample();
        WorkerMessageExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(dealer.getRole().getRoleId());
        criteria.andWorkMessagePhoneEqualTo(dealer.getDealerPhone());
        List<WorkerMessage> workerMessages = workerMessageDao.selectByExample(example);
        dealer.setWorkerMessageId(workerMessages.get(0).getWorkerMessageId());
        // emp
        dealer.setSoftdel(1);
        return dealerDao.insertSelective(dealer);
    }
    /**
     * @title 修改经销商信息
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @Override
    public Integer updateDealer(Dealer dealer) {
        return dealerDao.updateByPrimaryKeySelective(dealer);
    }
    /**
     * @title 删除经销商
     * @description 根据经销商的id软删除经销商
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer removeDealerDo(Dealer dealer) {
        dealer.setSoftdel(0);
        return dealerDao.updateByPrimaryKeySelective(dealer);
    }
    /**
     * @title 经销商列表查询
     * @description 组合查询，列表查询
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public List<Dealer> selectDealerList(LayUiPageBean pageBean, Dealer dealer, Date staData, Date endDate) {
        DealerExample dealerExample=new DealerExample();
        DealerExample.Criteria dealerExampleCriteria = dealerExample.createCriteria();
        //根据名字模糊查询
        if (dealer.getDealerName() != null && !"".equals(dealer.getDealerName())){
            dealerExampleCriteria.andDealerNameLike("%"+dealer.getDealerName()+"%");
        }
        //根据入职日期查询
        if(staData != null && !"".equals(staData)){
            dealerExampleCriteria.andDealerRegDateGreaterThanOrEqualTo(staData);
        }
        if (endDate != null && !"".equals(endDate)){
            dealerExampleCriteria.andDealerRegDateLessThanOrEqualTo(endDate);
        }

        //设置页面信息
        if(pageBean.getLimit() != null
                && !"".equals(pageBean.getLimit() )
                && pageBean.getPage() != null
                && !"".equals(pageBean.getPage())){
            dealerExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            dealerExample.setLimit(pageBean.getLimit().intValue());
        }


        List<Dealer> dealerList = dealerDao.selectByExample(dealerExample);
        //嵌套查询
        for(int i=0;i<dealerList.size();i++){
            dealerList.set(i,setClass(dealerList.get(i)));
        }

        return dealerList;
    }
    /**
     * @title 经销商查询
     * @description 根据编号进行查询
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Dealer selectDealer(Integer dealerId) {
        return setClass(dealerDao.selectByPrimaryKey(dealerId));
    }

    @Override
    public List<Dealer> getAll() {

        return dealerDao.selectByExample(null);
    }

    @Override
    public Integer getCount(Dealer dealer, Date staData , Date endDate) {
        DealerExample dealerExample=new DealerExample();
        DealerExample.Criteria dealerExampleCriteria = dealerExample.createCriteria();
        //根据名字模糊查询
        if (dealer.getDealerName() != null && !"".equals(dealer.getDealerName())){
            dealerExampleCriteria.andDealerNameLike("%"+dealer.getDealerName()+"%");
        }
        //根据入职日期查询
        if(staData != null && !"".equals(staData)){
            dealerExampleCriteria.andDealerRegDateGreaterThanOrEqualTo(staData);
        }
        if (endDate != null && !"".equals(endDate)){
            dealerExampleCriteria.andDealerRegDateLessThanOrEqualTo(endDate);
        }
        Long count = dealerDao.countByExample(dealerExample);
        return count.intValue();
    }

    /**
         * @title 根据电话查询用户是否为经销商
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public Dealer isDealer(String dealerPhone) {
        DealerExample dealerExample=new DealerExample();
        DealerExample.Criteria criteria = dealerExample.createCriteria();
        if (dealerPhone!=null&&!"".equals(dealerPhone)){
            criteria.andDealerPhoneEqualTo(dealerPhone);
        }
        List<Dealer> dealers = dealerDao.selectByExample(dealerExample);
        return
                dealers.get(0);
    }

    /**
     * @title 设置添加实体属性
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    public Dealer setClass(Dealer dealer){
        //加入员工信息
        dealer.setWorkerMessage(workerMessageDao.selectByPrimaryKey(dealer.getWorkerMessageId()));
        return  dealer;
    }

}
